# --- Libraries --- #
library(haven)
library(psych)
library(texreg)
library(ggeffects)
library(plyr)
library(dplyr)
library(DirectEffects)
library(ggplot2)
library(lavaan)
set.seed(1912)

setwd("~/Dropbox/democratic peace/analysis")


# --- Data --- #
qualtrics_data <- readr::read_csv("data/qualtrics_survey/qualtrics_raw_data.csv")
qualtrics_data <- subset(qualtrics_data, gc == "1") # gc = "good complete"
dim(qualtrics_data)


{sdo_rev_code <- apply(revalue(cbind(qualtrics_data$sdo1, qualtrics_data$sdo2),
                               c("Strongly disagree" = 1,
                                 "Disagree" = 2,
                                 "Somewhat disagree" = 3,
                                 "Neither agree nor disagree" = 4,
                                 "Somewhat agree" = 5,
                                 "Agree" = 6,
                                 "Strongly agree" = 7)), 2, as.numeric)
  
  rwa_rev_code <- apply(revalue(cbind(qualtrics_data$RWA1, qualtrics_data$RWA2, qualtrics_data$RWA3,
                                      qualtrics_data$RWA4, qualtrics_data$RWA5, qualtrics_data$RWA6, 
                                      qualtrics_data$RWA7),
                                c("Strongly disagree" = 1,
                                  "Disagree" = 2,
                                  "Somewhat disagree" = 3,
                                  "Neither agree nor disagree" = 4,
                                  "Somewhat agree" = 5,
                                  "Agree" = 6,
                                  "Strongly agree" = 7)), 2, as.numeric)
  
  race_resent_rev_code <- apply(revalue(cbind(qualtrics_data$RR1, qualtrics_data$RR2, qualtrics_data$RR3, qualtrics_data$RR4),
                                        c("Strongly disagree" = 1,
                                          "Disagree" = 2,
                                          "Somewhat disagree" = 3,
                                          "Neither agree nor disagree" = 4,
                                          "Somewhat agree" = 5,
                                          "Agree" = 6,
                                          "Strongly agree" = 7)), 2, as.numeric)
  
  ethno_rev_code <- apply(revalue(cbind(qualtrics_data$ethnocent1, qualtrics_data$ethnocent2, qualtrics_data$ethnocentr3, 
                                        qualtrics_data$ethnocentr4, qualtrics_data$ethnocent5, qualtrics_data$ethnocent6,
                                        qualtrics_data$ethnocent7),
                                  c("Strongly disagree" = 1,
                                    "Disagree" = 2,
                                    "Somewhat disagree" = 3,
                                    "Neither agree nor disagree" = 4,
                                    "Somewhat agree" = 5,
                                    "Agree" = 6,
                                    "Strongly agree" = 7)), 2, as.numeric)
}


qualtrics_df <- 
  data.frame(order = ifelse(qualtrics_data$order == 1, "nukes_first", "invasion_first"),
             
             nukes_trt = coalesce(qualtrics_data$FL_39_DO,qualtrics_data$FL_7_DO),
             nukes_race = gsub(".*_", "", coalesce(qualtrics_data$FL_39_DO,qualtrics_data$FL_7_DO)),
             nukes_regime = gsub(".*_", "", sub("([A-Za-z]+_[A-Za-z]+).*", "\\1", coalesce(qualtrics_data$FL_39_DO,qualtrics_data$FL_7_DO))),
             nukes_strike = as.numeric(revalue(qualtrics_data$support_nukes, c("Oppose strongly" = 1,
                                                                               "Oppose" = 2,
                                                                               "Oppose somewhat" = 3,
                                                                               "Neither favor nor oppose" = 4,
                                                                               "Favor somewhat" = 5,
                                                                               "Favor" = 6,
                                                                               "Favor strongly" = 7))),
             coop_int = rowSums(apply(revalue(cbind(qualtrics_data$fp_orient_block1_1, qualtrics_data$fp_orient_block2_1, qualtrics_data$fp_orient_block2_3),
                                              c("Disagree strongly" = 1,
                                                "Disagree" = 2,
                                                "Neither agree nor disagree" = 3,
                                                "Agree" = 4,
                                                "Agree strongly" = 5)), 2, as.numeric)),
             mil_int = rowSums(apply(revalue(cbind(qualtrics_data$fp_orient_block1_2, qualtrics_data$fp_orient_block2_2, qualtrics_data$fp_orient_block3_2),
                                             c("Disagree strongly" = 1,
                                               "Disagree" = 2,
                                               "Neither agree nor disagree" = 3,
                                               "Agree" = 4,
                                               "Agree strongly" = 5)), 2, as.numeric)),
             iso_int = rowSums(apply(revalue(cbind(qualtrics_data$fp_orient_block1_3, qualtrics_data$fp_orient_block3_1, qualtrics_data$fp_orient_block3_3),
                                             c("Disagree strongly" = 1,
                                               "Disagree" = 2,
                                               "Neither agree nor disagree" = 3,
                                               "Agree" = 4,
                                               "Agree strongly" = 5)), 2, as.numeric)),
             sdo = rowSums(cbind(sdo_rev_code[,1],(8-sdo_rev_code[,2]))),
             rwa = rowSums(cbind(rwa_rev_code[,1],rwa_rev_code[,2], rwa_rev_code[,3], 
                                 (8-rwa_rev_code[,4]), rwa_rev_code[,5], rwa_rev_code[,6], (8-rwa_rev_code[,7]))),
             race_resent = rowSums(cbind(race_resent_rev_code[,1],(8-race_resent_rev_code[,2]), 
                                         race_resent_rev_code[,3], (8-race_resent_rev_code[,4]))),
             ethno = rowSums(cbind(ethno_rev_code[,1], ethno_rev_code[,2], ethno_rev_code[,3],
                                   ethno_rev_code[,4], ethno_rev_code[,5], (8-ethno_rev_code[,6]),
                                   ethno_rev_code[,7])),
             ethno1 = ethno_rev_code[,1],
             ethno2 = ethno_rev_code[,2],
             ethno3 = ethno_rev_code[,3],
             ethno4 = ethno_rev_code[,4], 
             ethno5 = ethno_rev_code[,5], 
             ethno6 = (8-ethno_rev_code[,6]),
             ethno7 = ethno_rev_code[,7],
             
             age = (2022-as.numeric(qualtrics_data$age)),
             gender = ifelse(qualtrics_data$gender == "Male", "male", "nonmale"),
             race = ifelse(qualtrics_data$race == "White / Caucasian", "white", "nonwhite"),
             pid = as.numeric(revalue(qualtrics_data$pid, c("Strongly Republican" = 7,
                                                            "Republican" = 6,
                                                            "Independent but lean Republican" = 5,
                                                            "Independent" = 4,
                                                            "Independent but lean Democrat" = 3,
                                                            "Democrat" = 2,
                                                            "Strongly Democrat" = 1))),
             ideology = as.numeric(revalue(qualtrics_data$ideology, c("Extremely conservative" = 7,
                                                                      "Conservative" = 6,
                                                                      "Slightly conservative" = 5,
                                                                      "Moderate or 'middle of the road'" = 4,
                                                                      "Slightly liberal" = 3,
                                                                      "Liberal" = 2,
                                                                      "Extremely liberal" = 1))),
             education = ifelse(qualtrics_data$education %in% c("4-year college", "Graduate degree"), "college", "noncollege"),
             attn_check = qualtrics_data$attncheck
  )









